package com.hxc.common.controller;

import com.hxc.common.bean.Document;
import com.hxc.common.common.ResultVO;
import com.hxc.common.service.MarkDownService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/markDown")
public class MarkDownController {
    @GetMapping("/searchProblem")
    public ResultVO<List<String>> searchMarkdown(@RequestParam("searchKey") String searchKey)
        throws Exception {
        // 获取Git仓库中的Markdown文档列表
        try {
            List<String> markdownFiles = new MarkDownService().getGitDataFilePath();
            List<String> results = new ArrayList<>();
            if (StringUtils.isEmpty(searchKey)) {
                results.addAll(markdownFiles);
            } else {
                for (String path:markdownFiles) {
                    // 如果标题包含检索关键字加入列表
                    if (path.contains(searchKey)) {
                        results.add(path);
                    } else {
                        // 判断具体内容是否包含关键字，是则加入列表
                        if (new MarkDownService().isContainSearchKeyForContent(searchKey,path)) {
                            results.add(path);
                        }
                    }
                }
            }
            return new ResultVO<>(0,"OK",results);
        }catch (Exception e) {
            return new ResultVO<>(1,e.getMessage());
        }
    }

    @GetMapping("test")
    public ResultVO<List<String>> test() throws Exception{
        List<String> filepathList = new MarkDownService().findGitMarkDown();

        return new ResultVO<>(filepathList);
    }

    @PostMapping("test1")
    public ResultVO<List<String>> test1(@RequestBody List<String> filePathList) throws Exception{
        List<String> fileContentList = new MarkDownService().getGitDataFileContent(filePathList);
        return new ResultVO<>(fileContentList);
    }

    @GetMapping("findMarkDownBypath")
    public ResultVO<String> findMarkDownBypath(@RequestParam("path")String path) throws Exception {
        try {
            return new ResultVO<>(new MarkDownService().findMarkDownBypathWithImage(path));
        }catch (Exception e) {
            return new ResultVO<>(1,e.getMessage());
        }
    }
}
